/*=============================================================================

MANAGER IDLE MODE MODULE

FILE: SYSmgr_S_BNK.cpp

DESCRIPTION

Manager Measurement Mode Implementation

EXTERNALIZED FUNCTIONS


Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

EDIT HISTORY

---------  -------  -------------------------------------------------------
when       who     what, where, why
---------  -------  -------------------------------------------------------
06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
HEADER INCLUSION
=============================================================================*/
#include "SYSmgr.h"

/*=============================================================================
MACRO DEFINITION
=============================================================================*/
#define GETSTEP()		(gStep)
#define SETSTEP(s)		((gStep) = (s))

/*=============================================================================
CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
LOCALIZED VARIABLE DEFINITION
=============================================================================*/
static dword	gStep = STATE_INIT_STEP;

typedef enum
{
	HOME_INIT_STEP = STATE_INIT_STEP,

	// GLS
	HOME_GLSREQ_STEP,

	// X-HOME
	HOME_XPREP_STEP,
	HOME_XHOME_STEP,
	HOME_XWAIT_STEP,

	// Y-PREP
	HOME_YPREP_STEP,
	HOME_YHOME_STEP,
	HOME_YWAIT_STEP,

	// Z-PREP
	HOME_ZPREP_STEP,
	HOME_ZHOME_STEP,
	HOME_ZWAIT_STEP,


	HOME_DONE_STEP,

	// BASIC
	HOME_ERR_STEP,
	HOME_EXIT_STEP
} RESET_HOMEStep_e;

/*=============================================================================
LOCALIZED FUNCTION DEFINITION
=============================================================================*/

/*=============================================================================
EVENT PROCESSING
=============================================================================*/
dword SYSmgr::RESET_ProcHOME(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	boolean				reqStop = FALSE;
	static boolean		sysErr = FALSE;
	dword				evtID, event;

	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	// [Internal Command Response]
	if(IS_CMD_EVENT(dwType))
	{
		switch(event)
		{
		case STATE_STOP_C:
			{
				m_pMot->HomeStop();
				SETSTEP(HOME_EXIT_STEP);
				reqStop = TRUE;
				break;
			}
		}
	}
	else if(dwType == SYS_MGR)
	{
		return	FALSE;
	}

	// [Sequence Control]
	switch(GETSTEP())
	{
	case HOME_INIT_STEP:
	{
		sysErr = FALSE;

		LOGINFO("=====================");
		LOGINFO(" Motion Homing Start");
		LOGINFO("=====================");
		NOTI_REPORT("Homing", "Homing Start.");

		// Stop All-axis
		HMot_StopAll();

		// Clear home
		SETSTEP(HOME_GLSREQ_STEP);
		break;
	}
////---------------------------------
//// GLS-TASK
////---------------------------------
	case HOME_GLSREQ_STEP:
	{
		switch(m_homeReq)
		{
		case MOT_AXIS_Y:
		{
			if(m_pMot->HomeClear(MOT_AXIS_Y) == FALSE)
			{
				ERR_REPORT("Homing Error", "Homing Init fail !");
				sysErr = TRUE;
				SETSTEP(HOME_EXIT_STEP);
			}
			else
			{
				SETSTEP(HOME_YPREP_STEP);
			}
			break;
		}
		case MOT_AXIS_X:
		{
			if(m_pMot->HomeClear(MOT_AXIS_X) == FALSE)
			{
				ERR_REPORT("Homing Error", "Homing Init fail !");
				sysErr = TRUE;
				SETSTEP(HOME_EXIT_STEP);
			}
			else
			{
				SETSTEP(HOME_XPREP_STEP);
			}
			break;
		}
		case MOT_AXIS_ALL:
		default:
		{
			if(m_pMot->HomeClear() == FALSE)
			{
				ERR_REPORT("Homing Error", "Homing Init fail !");
				sysErr = TRUE;
				SETSTEP(HOME_EXIT_STEP);
			}
			else
			{
				SETSTEP(HOME_YPREP_STEP);
			}
			break;
		}
		}
		break;
	}
//---------------------------------
// X-HOME
//---------------------------------
	case HOME_XPREP_STEP:
	{
		LOGINFO("X-Axis Homing Start !");
		SETSTEP(HOME_XHOME_STEP);
		break;
	}
	case HOME_XHOME_STEP:
	{
		m_pMot->HOMEset(MOT_AXIS_X, HMot_TblPosition(HMOT_X_HOFFSET_POS));
		if(m_pMot->Home(MOT_AXIS_X) == FALSE)
		{
			ERR_REPORT("Homing Error", "X-Axis Homing is impossible !");
			SETSTEP(HOME_ERR_STEP);
		}
		else
		{
			SETSTEP(HOME_XWAIT_STEP);
		}
		break;
	}
	case HOME_XWAIT_STEP:
	{
		if(dwType != HMOT_DEV)
			break;

		switch(event)
		{
		case MOT_HOME_DONE_E:
		{
			if(wParam == MOT_AXIS_X)
			{
				LOGINFO("X-Axis Homing Complete !");
				HMot_EgearEnable(MOT_AXIS_X, TRUE);
				if(m_homeReq == MOT_AXIS_ALL)
				{
					SETSTEP(HOME_YPREP_STEP);
				}
				else
				{
					SETSTEP(HOME_DONE_STEP);
				}
			}
			break;
		}
		case MOT_HOME_ERR_E:
		{
			LOGERR("X-Axis Homing Error !");
			SETSTEP(HOME_ERR_STEP);
			break;
		}
		}
		break;
	}
//---------------------------------
// Y-HOME
//---------------------------------
	case HOME_YPREP_STEP:
	{
		//HIO_Delay(1000);
		LOGINFO("Y-Axis Homing Start !");
		SETSTEP(HOME_YHOME_STEP);
		break;
	}
	case HOME_YHOME_STEP:
	{
		m_pMot->HOMEset(MOT_AXIS_Y, HMot_TblPosition(HMOT_Y_HOFFSET_POS));
		if(m_pMot->Home(MOT_AXIS_Y) == FALSE)
		{
			ERR_REPORT("Homing Error", "Y-Axis Homing is impossible !");
			SETSTEP(HOME_ERR_STEP);
		}
		else
		{
			SETSTEP(HOME_YWAIT_STEP);
		}
		break;
	}
	case HOME_YWAIT_STEP:
	{
		if(dwType != HMOT_DEV)
			break;

		switch(event)
		{
		case MOT_HOME_DONE_E:
		{
			if(wParam == MOT_AXIS_Y)
			{
				LOGINFO("Y-Axis Homing Complete !");
				HMot_EgearEnable(MOT_AXIS_Y, TRUE);
				SETSTEP(HOME_DONE_STEP);
			}
			break;
		}
		case MOT_HOME_ERR_E:
		{
			LOGERR("Y-Axis Homing Error !");
			SETSTEP(HOME_ERR_STEP);
			break;
		}
		}
		break;
	}
	case HOME_DONE_STEP:
	{
		LOGINFO("Motion Homing Complete");
		NOTI_REPORT("INFORMATION", "Homing Complete.");
		SETSTEP(HOME_EXIT_STEP);
		break;
	}
	case HOME_ERR_STEP:
	{
		sysErr = TRUE;
		SETSTEP(HOME_EXIT_STEP);
		break;
	}
	case HOME_EXIT_STEP:
	{
		SETSTEP(STATE_INIT_STEP);
		if(reqStop)
		{
			m_pMot->HomeStop();
		}
		if(sysErr)
		{
			sysErr = FALSE;
			State_Change(IDL_S, IDL_ERR_S);
		}
		else
		{
			State_Change(IDL_S, IDL_IDL_S);
		}
		break;
	}
	}
	return	FALSE;
}
